package com.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //从login.jsp页面上获取用户名和密码
        String uname = req.getParameter("uname");
       String pwd  = req.getParameter("pwd");

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        if (uname ==null || uname.length() ==0  || pwd == null || pwd.length() == 0) {
            resp.sendRedirect("login.jsp");
        } else {

            //从session对象获取来自注册页面的数据
            HttpSession session = req.getSession();
            String registerUname = (String )session.getAttribute("uname");
            String registerPwd = (String )session.getAttribute("pwd");
            if (uname.equals(registerUname) && pwd.equals(registerPwd)) {

                out.println(uname +": 欢迎你，你已成功登录,说明你注册时的用户名和密码与登陆时的用户名密码一致");
            }else  {
                out.println("注册时用户名和密码与登陆时的用户名密码不一致");
            }


        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp);
        System.out.println("调用了doPost方法");
        doGet(req,resp);





    }
}
